package site.lyun.dao;

import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import site.lyun.entity.Goods;
import site.lyun.entity.GoodsInf;
import site.lyun.entity.PageBean;
import site.lyun.entity.User;
import java.util.ArrayList;
import java.util.List;

public class GoodsDaoImpl extends HibernateDaoSupport implements GoodsDao{

    @Override
    public boolean GoodsUpload(Goods goods) {
        try {
            HibernateTemplate hibernateTemplate = this.getHibernateTemplate();
            hibernateTemplate.saveOrUpdate(goods);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public PageBean<Goods> GoodsFind(int page) {
        HibernateTemplate hibernateTemplate = this.getHibernateTemplate();
        List<Goods> list = (List<Goods>)hibernateTemplate.find("from Goods order by goodsId desc");
        List<Goods> myList = new ArrayList<>();
        PageBean pageBean = new PageBean();


        if(!list.isEmpty()){
            for(int i=(page-1)*20;i<list.size()&&i<page*20;i++){
                Goods tmp = list.get(i);
                myList.add(tmp);
            }
            int totalCount = list.size();
            int totalPage = totalCount%20==0 ? totalCount/20:totalCount/20+1;
            int start = page-4<=1 ? 1:page-4;
            int end = page+4>=totalPage? totalPage:page+4;

            pageBean.setStart(start);
            pageBean.setEnd(end);
            pageBean.setPage(page);
            pageBean.setTotalCount(totalCount);
            pageBean.setTotalPage(totalPage);
            pageBean.setList(myList);
            pageBean.setLimit(myList.size());
        }

        return pageBean;
    }

    @Override
    public PageBean<GoodsInf> GoodsTradeInf(int page) {
        HibernateTemplate hibernateTemplate = this.getHibernateTemplate();
        List<GoodsInf> list = (List<GoodsInf>)hibernateTemplate.find("from GoodsInf order by infId desc");
        List<GoodsInf> myList = new ArrayList<>();
        PageBean pageBean = new PageBean();

        if(!list.isEmpty()){
            for(int i=(page-1)*20;i<list.size()&&i<page*20;i++){
                GoodsInf tmp = list.get(i);
                myList.add(tmp);
            }
            int totalCount = list.size();
            int totalPage = totalCount%20==0 ? totalCount/20:totalCount/20+1;
            int start = page-4<=1 ? 1:page-4;
            int end = page+4>=totalPage? totalPage:page+4;

            pageBean.setStart(start);
            pageBean.setEnd(end);
            pageBean.setPage(page);
            pageBean.setTotalCount(totalCount);
            pageBean.setTotalPage(totalPage);
            pageBean.setList(myList);
            pageBean.setLimit(myList.size());
        }

        return pageBean;
    }

    @Override
    public Goods GoodsShow(int goodsId) {
        HibernateTemplate hibernateTemplate = this.getHibernateTemplate();
        Goods goods = hibernateTemplate.get(Goods.class,goodsId);
        if(goods != null){
            return goods;
        }
        return null;
    }

    @Override
    public boolean GoodsBuy(Goods goods, User user) {
        HibernateTemplate hibernateTemplate = this.getHibernateTemplate();
        GoodsInf goodsInf = new GoodsInf();
        goodsInf.setGoodsName(goods.getGoodsName());
        goodsInf.setGoodsValue(goods.getGoodsValue());
        goodsInf.setSellerName(goods.getSellerName());
        goodsInf.setCustomerName(user.getUserName());
        goodsInf.setDate(new java.sql.Date(new java.util.Date().getTime()));
        goodsInf.setTime(new java.sql.Time(new java.util.Date().getTime()));
        try {
            hibernateTemplate.saveOrUpdate(goodsInf);
            hibernateTemplate.delete(goods);
        } catch (Exception e){
            return false;
        }
        return true;
    }
}
